#!/usr/bin/env python
# Copyright(c) 2010 Nuttachat Wisittipanit. All rights reserved.

import xlwt,interval


class MSTATReport(object):
    
    def __init__(self,**kwargs):
        #Process only one datasetname at a time
        self.output_file    = ''
        self.pthreshold     = 0.05 #For Metastats
        #self.dset  -> {'gutmtpslhpcr_Colon_Lumen__Crohn_Colon__Crohn_Lumen': RESULTS isstance, ..}
        self.dset           = {}
        #self.dsort -> List of datasetname -> ['gutmtpslhpcr_Colon_Lumen__Crohn_Colon__Crohn_Lumen', ..] 
        self.dsort          = []
        self.nameabr        = []
        self.__dict__.update(kwargs)
        
    def xcelRecord(self):
        #Excel properties
        self.book           = xlwt.Workbook()
        col_width           = 5000
        row_list            = ['Features','P-Value']                                
        self.sheet          = {}
        #self.namesort -> ['gutmtpslhpcr_Entire','gutmtpslhpcr_Colon_Lumen',..]
        for i in range(len(self.dsort)):
            dname           = self.dsort[i]
            #subname -> 'Crohn_Colon__Crohn_Lumen'
            nameAbr         = self.nameabr[i]
            #result_obj -> RESULTS instance
            result_obj      = self.dset[dname]
            results_mstat   = result_obj.results_mstat
            mdata           = results_mstat.orgnresult
            if mdata == None:
                continue 
            self.sheet[i]               = self.book.add_sheet(nameAbr)
            #Set Width of column 0
            self.sheet[i].col(0).width  = col_width
            self.sheet[i].row(0).write(0,dname)
            self.sheet[i].row(1).write(0,'Positive')
            self.sheet[i].row(2).write(0,'Negative')
            cur_col         = 1
            cur_row         = 0
            self.sheet[i].col(cur_col).width        = col_width
            self.sheet[i].col(cur_col+1).width      = col_width
            mainname        = mdata.datasetname                   # 'gutmtpslhpcr_Colon_Lumen__Crohn_Colon__Crohn_Lumen'
            subname         = '__'.join(mainname.split('__')[1:]) # 'Crohn_Colon__Crohn_Lumen'
            xname           = subname.split('__')
            if 'Healthy Control' in xname:
                Positive    = xname[0] if xname[0] != 'Healthy Control' else xname[1]
                Negative    = xname[1] if xname[1] == 'Healthy Control' else xname[0]
            else:
                Positive    = xname[0]
                Negative    = xname[1]
            self.sheet[i].row(cur_row+1).write(cur_col      ,Positive       )
            self.sheet[i].row(cur_row+2).write(cur_col      ,Negative       )
            self.sheet[i].row(cur_row+3).write(cur_col      ,row_list[0]    )
            self.sheet[i].row(cur_row+3).write(cur_col+1    ,row_list[1]    )
            temprow         = cur_row + 4
            pselect         = [u for u in mdata.MSTATp_sort_fisher \
                               if u[1] in interval.Interval(0,self.pthreshold,lower_closed = True,upper_closed = True)] \
                               if mdata.MSTATp_sort_fisher != None else [] #[(feature1,p1),(feature2,p2), ..] sorted from low-high
            for j in pselect:
                self.sheet[i].row(temprow).write(cur_col,j[0])
                self.sheet[i].row(temprow).write(cur_col+1,'%.4f' % j[1])
                temprow += 1
            cur_col     += 2
        if len(self.dset) != 0:
            self.book.save(self.output_file)
            
            
            